//    Copyright (C) Kherty.  All rights reserved.
#region

using System;
using OpenLS.Core;
using OpenLS.Core.Serialization;
using OpenLS.Spreadsheet.Serialization;

#endregion

namespace OpenLS.Spreadsheet
{
    internal class ChartSheetPageSetup : IOfficeXmlSerializable
    {
        private const string xmlName = "pageSetup";
        private Boolean _blackAndWhite;
        private UInt32 _copies;
        private Boolean _draft;
        private UInt32 _firstPageNumber;
        private UInt32 _horizontalDpi;
        private Orientation _orientation;
        private UInt32 _paperSize;
        private Boolean _useFirstPageNumber;
        private Boolean _usePrinterDefaults;
        private UInt32 _verticalDpi;

        private byte[] data;

        #region IOfficeXmlSerializable Members

        public void ReadXml(ReadContext context)
        {
            using (var c = context.Read(xmlName))
            {
                _paperSize = c.GetOptionalUInteger("paperSize", 1);
                _firstPageNumber = c.GetOptionalUInteger("firstPageNumber", 1);
                _orientation = c.GetOptionalEnum("orientation", Orientation.Default);
                _usePrinterDefaults = c.GetOptionalBoolean("usePrinterDefaults", true);
                _blackAndWhite = c.GetOptionalBoolean("blackAndWhite", false);
                _draft = c.GetOptionalBoolean("draft", false);
                _useFirstPageNumber = c.GetOptionalBoolean("useFirstPageNumber", false);
                _horizontalDpi = c.GetOptionalUInteger("horizontalDpi", 600);
                _verticalDpi = c.GetOptionalUInteger("verticalDpi", 600);
                _copies = c.GetOptionalUInteger("copies", 1);
                var rid = c.GetOptionalString(OfficeConstants.RelationshipNamespace + "id");
                if (rid != null)
                {
                    using (var stream = c.OpenRelatedStream(rid))
                    {
                        data = new byte[stream.Length];
                        stream.Read(data, 0, data.Length);
                    }
                }
            }
        }

        public void WriteXml(WriteContext context)
        {
            using (var c = context.Write(xmlName))
            {
                c.SetOptionalUInteger("paperSize", _paperSize, 1);
                c.SetOptionalUInteger("firstPageNumber", _firstPageNumber, 1);
                c.SetOptionalEnum("orientation", _orientation, Orientation.Default);
                c.SetOptionalBoolean("usePrinterDefaults", _usePrinterDefaults, true);
                c.SetOptionalBoolean("blackAndWhite", _blackAndWhite, false);
                c.SetOptionalBoolean("draft", _draft, false);
                c.SetOptionalBoolean("useFirstPageNumber", _useFirstPageNumber, false);
                c.SetOptionalUInteger("horizontalDpi", _horizontalDpi, 600);
                c.SetOptionalUInteger("verticalDpi", _verticalDpi, 600);
                c.SetOptionalUInteger("copies", _copies, 1);
                string rid;
                if (data != null)
                {
                    using (
                        var stream = c.CreateRelatedStream("printerSetting{0}.bin",
                                                           SpreadsheetConstants.PrinterSettingsContentType,
                                                           SpreadsheetConstants.PrinterSettings, out rid))
                    {
                        stream.Write(data, 0, data.Length);
                        c.SetString(OfficeConstants.RelationshipNamespace + "id", rid);
                    }
                }
            }
        }

        #endregion
    }
}